package wetogether.travelinfo.control;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import wetogether.travelinfo.service.InputTravelInfoService;
import wetogether.travelinfo.vo.TravelCommentVO;
import wetogether.travelinfo.vo.TravelFrVO;
import wetogether.travelinfo.vo.TravelImageVO;
import wetogether.travelinfo.vo.TravelInfoVO;
import wetogether.travelinfo.vo.TravelVacancyVO;
import wetogether.travelinfo.vo.TravelZoneVO;

public class InputTravelInfoController extends MultiActionController{
	private InputTravelInfoService inputTravelInfoService;
	public void setInputTravelInfoService(InputTravelInfoService inputTravelInfoService){
		this.inputTravelInfoService=inputTravelInfoService;
		System.out.println("InputTravelInfoService injection....");
	}
	/**
	 * request를 VO로 bind하여 넘어온 값을 받은 다음
	 * 0. 지역 정보를 입력한다. inputTravelZone
	 */
	public ModelAndView inputTravelZone(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infoinput_result");
		TravelZoneVO zoneVO=new TravelZoneVO();
		try{
			bind(request,zoneVO);
			inputTravelInfoService.insertTravelZone(zoneVO);
			mv.addObject("insert",zoneVO.getLocation()+" insert OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 1. 여행지 정보를 입력한다. inputTravelInfo
	 */
	public ModelAndView inputTravelInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infoinput_result");
		TravelInfoVO tVO=new TravelInfoVO();
		try{
			bind(request,tVO);
			inputTravelInfoService.insertTravelInfo(tVO);
			mv.addObject("insert",tVO.getPlace()+" insert OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 2. 해당 여행지의 이미지 입력 inputPlaceImage
	 */
	public ModelAndView inputPlaceImage(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infoinput_result");
		TravelImageVO imgVO=new TravelImageVO();
		try{
			bind(request,imgVO);
			inputTravelInfoService.insertPlaceImage(imgVO);
			mv.addObject("insert",
					imgVO.getPlace()+"의 "+imgVO.getGallery()+" 이미지 insert OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 3. 해당 여행지의 숙박정보 입력 inputPlaceVacancy
	 */
	public ModelAndView inputPlaceVacancy(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infoinput_result");
		TravelVacancyVO vacancyVO=new TravelVacancyVO();
		try{
			bind(request,vacancyVO);
			inputTravelInfoService.insertPlaceVacancy(vacancyVO);
			mv.addObject("insert",
					vacancyVO.getPlace()+"의 "+vacancyVO.getStay()+" 숙박정보 insert OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 4. 해당 여행지의 맛집정보 입력 inputPlaceFr
	 */
	public ModelAndView inputPlaceFr(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infoinput_result");
		TravelFrVO frVO=new TravelFrVO();
		try{
			bind(request,frVO);
			inputTravelInfoService.insertPlaceFr(frVO);
			mv.addObject("insert",
					frVO.getPlace()+"의 "+frVO.getRestaurant()+" 맛집정보 insert OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 5. 공지사항 입력 inputPlaceNotice
	 */
	public ModelAndView inputPlaceNotice(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infoinput_result");
		TravelInfoVO tVO=new TravelInfoVO();
		String place=request.getParameter("place");
		String notice=request.getParameter("notice");
		System.out.println("place:"+place);
		System.out.println("notice:"+notice);
		if(!place.equals("")&&!notice.equals("")){
			tVO.setPlace(place);
			tVO.setNotice(notice);
			try{
				inputTravelInfoService.insertPlaceNotice(tVO);
				mv.addObject("insert",
						tVO.getPlace()+"의 공지사항 insert OK!!");
			}catch(SQLException se){
				mv.addObject("Emessage",se.getMessage());
			}catch(Exception e){
				mv.addObject("Emessage", e.getMessage());
			}
		}else{
			mv.addObject("Emessage","Null값은 입력이 불가능 합니다!");
		}
		return mv;
	}
	/**
	 * 6. 해당 여행지의 댓글 입력 inputPlaceComment
	 */
	public ModelAndView inputPlaceComment(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("infoinput_result");
		TravelCommentVO commentVO=new TravelCommentVO();
		try{
			bind(request,commentVO);
			inputTravelInfoService.insertPlaceComment(commentVO);
			// 여행정보에 입력받은 추천점수를 입력시킨다.
			inputTravelInfoService.insertTravelInfoRecommend(commentVO);
			mv.addObject("insert",
					commentVO.getPlace()+"의 "+commentVO.getMemberid()+"님 댓글 insert OK!!");
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	////////////////////////////////////////////
	// Output에서 가져온 값
	/////////////////////////////////////////
	/**
	 * 존재하는 지역 리스트 findZoneList
	 */
	public ModelAndView findZoneList(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		String change=request.getParameter("change");
		mv.addObject("locationId",request.getParameter("locationId"));
		mv.setViewName("place"+change+"_input");
		try{
			List<TravelZoneVO> zoneList=inputTravelInfoService.findTravelZoneList();
			mv.addObject("zoneList",zoneList);
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 해당 지역에 존재하는 여행지 리스트 findPlaceList
	 */
	public ModelAndView findPlaceList(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("travelplace_list");
		int locationid=Integer.parseInt(request.getParameter("locationid"));
		String change=request.getParameter("change");
		mv.addObject("change",change);
		try{
			List<String> infoList=inputTravelInfoService.findTravelPlaceList(locationid);
			if(infoList.size()!=0){
				mv.addObject("infoList",infoList);
			}else{
				mv.addObject("Emessage",locationid+" 지역의 여행지가 존재하지 않습니다!");
			}
		}catch(SQLException se){
			mv.addObject("Emessage",se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
	/**
	 * 입력 전 이미 입력되어 있는 정보를 보여준다
	 */
	public ModelAndView getTravelInfoValue(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		ModelAndView mv=new ModelAndView();
		mv.setViewName("isinputinfo");
		String place=request.getParameter("place");
		String change=request.getParameter("change");
		mv.addObject("change",change);
		try{
			if(change.equals("image")){
				List<String> imgList=inputTravelInfoService.getPlaceImageList(place);
				if(imgList.size()!=0){
					mv.addObject("isinputinfo","존재하는 이미지 리스트 : "+imgList);
				}else{
					mv.addObject("isinputinfo","존재하는 이미지가 없습니다!");
				}
			}else if(change.equals("vacancy")){
				List<String> vList=inputTravelInfoService.isPlaceVacancy(place);
				if(vList.size()!=0){
					mv.addObject("isinputinfo","존재하는 숙박시설 리스트 : "+vList);
				}else{
					mv.addObject("isinputinfo","존재하는 숙박시설이 없습니다!");
				}
			}else if(change.equals("fr")){
				List<String> fList=inputTravelInfoService.isPlaceFr(place);
				if(fList.size()!=0){
					mv.addObject("isinputinfo","존재하는 맛집 리스트 : "+fList);
				}else{
					mv.addObject("isinputinfo","존재하는 맛집이 없습니다!");
				}
			}else if(change.equals("notice")){
				String notice=inputTravelInfoService.isPlaceNotice(place);
				if(notice!=null){
					mv.addObject("isinputinfo","공지가 이미 존재합니다! 공지내용 : '"+notice+"'");
					mv.addObject("flag",true);
				}else{
					mv.addObject("isinputinfo","공지 입력이 가능합니다!");
					mv.addObject("flag",false);
				}
			}
		}catch(SQLException se){
			mv.addObject("Emessage", se.getMessage());
		}catch(Exception e){
			mv.addObject("Emessage", e.getMessage());
		}
		return mv;
	}
}
